package com.gene.controller.file;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.gene.utils.AliyunOSSClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/gene-safe")
public class DowPdfConntroller {
    @RequestMapping(value = "/item/dowPdf")
    @ResponseBody
    public void itemUpload2(@RequestParam(value = "file") String fileName, HttpServletResponse response) throws Exception {
        OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
        OSSObject ossObject = ossClient.getObject("gene-sefa", fileName);
        try {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("application/pdf;charset=UTF-8");
            // 获取outputStream
            OutputStream outputStream = response.getOutputStream();
            // 获取inputStream
            InputStream is = ossObject.getObjectContent();
            // 下载文件
            byte[] bytes = new byte[2048];
            int length;
            while ((length = is.read(bytes)) > 0) {
                outputStream.write(bytes, 0, length);
            }
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
    }




    }
